package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.SettingManager;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class SettingController {

    @Autowired
    private SettingManager settingManager;

    // 查询通用设置
    @GetMapping("/users/settings")
    public ResponseEntity findSettingVo() {
        return settingManager.findSettingVo();
    }

    // 设置陌生人问题
    @PostMapping("/users/questions")
    public void setQuestion(@RequestBody Map<String, String> map) {
        String content = map.get("content");
        // 调用manager
        settingManager.setQuestion(content);
    }

    // 设置推送通知
    @PostMapping("/users/notifications/setting")
    public void setNotification(@RequestBody Notification notificationParam) {
        // 调用manager
        settingManager.setNotification(notificationParam);
    }

    // 分页查询黑名单
    @GetMapping("/users/blacklist")
    public ResponseEntity findBlckListByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "8") Integer pageSize) {
        // 调用manager
        return settingManager.findBlckListByPage(pageNum, pageSize);
    }

    // 移除黑名单
    @DeleteMapping("/users/blacklist/{uid}")
    public void deleteBlackList(@PathVariable("uid") Long blackUserId) {
        // 调用manager
        settingManager.deleteBlackList(blackUserId);
    }

    //修改手机号1：发送验证码
    @PostMapping(value = "/users/phone/sendVerificationCode",name = "other")
    public void sendCode(){
        //获取当前登录的手机号
        User user = UserHolder.get();
        String phone = user.getPhone();

        // System.out.println("发送验证码");
        settingManager.sendCode(phone);
    }

    //修改手机号2:验证验证码正确性
    @PostMapping(value = "/users/phone/checkVerificationCode",name = "other")
    public Map<String,Boolean> verifyCode(@RequestBody Map<String,String> map){
        String verificationCode = map.get("verificationCode");
        return settingManager.verifyCode(verificationCode);
    }
    //修改手机号3:保存修改后的新手机号
    @PostMapping(value = "/users/phone",name = "other")
    public void saveNewPhone(@RequestBody Map<String,String> map){
        //System.out.println("进不来？");
        String newPhone = map.get("phone");
        settingManager.saveNewPhone(newPhone);
    }
}
